package DAO;



import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.xml.crypto.Data;

import bean.*;
import conn.ConnectDatabase;
import service.IPetDao;
/**
 * 宠物操作接口实现类，实现宠物的增加、删除、更新、添加等操作
 * @author Administrator
 *
 */
public class PetbeanImp implements IPetDao {
	PreparedStatement ps = null;
	
	
	Date date = new Date();
	//得到一个timestamp格式的时间，存入mysql中的时间格式为"yyyy-MM-dd HH:mm:ss"
	Timestamp timestamp = new Timestamp(date.getTime());
	
	public boolean insert(Petbean p) {
		String sql ="insert into pet(id,name,type,birthday,own_name) values(?,?,?,?,?)";
		String sql1= "insert into history(description,data) values(?,?)";
		int n =0;
		try{
		ps = ConnectDatabase.getConnection().prepareStatement(sql);
		
		ps.setInt(1, p.getId());
		ps.setString(2, p.getName());
		ps.setString(3, p.getType());
		ps.setString(4, p.getBirthday());
		ps.setString(5, p.getOwn_name());
		n=ps.executeUpdate();
		ps = ConnectDatabase.getConnection().prepareStatement(sql1);
		ps.setString(1, "增加了一次宠物信息");
		ps.setTimestamp(2,timestamp);
		ps.executeUpdate();
		}catch (Exception e) {
		e.printStackTrace();
		}
		return n>0;
	}

	public boolean delete(Petbean p) {
		String sql ="delete from pet where id="+p.getId();
		String sql1= "insert into history(description,data) values(?,?)";
		int n=0;
		try{
		ps=ConnectDatabase.getConnection().prepareStatement(sql);
		n=ps.executeUpdate();
		ps = ConnectDatabase.getConnection().prepareStatement(sql1);
		ps.setString(1, "删除了一次宠物信息");
		ps.setTimestamp(2,timestamp);
		ps.executeUpdate();
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		
		return n>0;
	}

	public boolean update(Petbean p) {
		String sql = "update pet set name=?,type=?,birthday=?,own_name=? where id=?";
		String sql1= "insert into history(description,data) values(?,?)";
		int n = 0;
		try{
		ps=ConnectDatabase.getConnection().prepareStatement(sql);
		ps.setString(1, p.getName());
		ps.setString(2, p.getType());
		ps.setString(3, p.getBirthday());
		ps.setString(4, p.getOwn_name());
		ps.setInt(5, p.getId());
		n=ps.executeUpdate();
		ps = ConnectDatabase.getConnection().prepareStatement(sql1);
		ps.setString(1,  "更新了一次宠物信息");
		ps.setTimestamp(2,  timestamp);
		ps.executeUpdate();		
		}
		catch(Exception e){
			e.printStackTrace();
		}
		return n>0;
	}

	//list排序浏览
	public List<Petbean> queryPet(Map<String, Object> map) {
		
		StringBuffer sql= new StringBuffer("select * from pet where 1 =1");
		List<Petbean> list = new ArrayList<Petbean>();
		try{
		ps = ConnectDatabase.getConnection().prepareStatement(sql.toString());
		ResultSet rs = ps.executeQuery();
		while(rs.next()){
			Petbean p = new Petbean();
			p.setId(rs.getInt("id"));
			p.setName(rs.getString("name"));
			p.setType(rs.getString("type"));
			p.setBirthday(rs.getString("birthday"));
			p.setOwn_name(rs.getString("own_name"));
			list.add(p);
		}
	
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		
		return list;
	}

//查找宠物
	public Petbean queryById(Petbean p) {
		String sql1= "insert into history(description,data) values(?,?)";
		Petbean pet = null;
		String sql = "select * from pet where id="+p.getId();
		try{
		ps = ConnectDatabase.getConnection().prepareStatement(sql);
		ResultSet rs = ps.executeQuery();
		if(rs.next()){
			pet  = new Petbean();
			pet.setId(rs.getInt("id"));
			pet.setName(rs.getString("name"));
			pet.setType(rs.getString("type"));
			pet.setBirthday(rs.getString("birthday"));
			pet.setOwn_name(rs.getString("own_name"));
			ps = ConnectDatabase.getConnection().prepareStatement(sql1);
			ps.setString(1,  "查找了一次宠物信息");
			ps.setTimestamp(2,  timestamp);
			ps.executeUpdate();				
				
		}
		}catch(Exception e){
			e.printStackTrace();
		}
		return pet;
	}
}


